var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        questionId:'',
        classId:'',
        moduleName:''
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
        this.params.questionId = Pub.getSession('questionId');
        this.params.moduleName = Pub.getSession('moduleName');
    },
    getData:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            questionId:_this.params.questionId
        };
        Api.adminquestioninfo(paramsObj, function(data){
            console.log(data);
            $('#helpTitle').val(data.title);
            ue.ready(function() {//编辑器初始化完成再赋值
              ue.setContent(data.content);  //赋值给UEditor
            });
             setTimeout(function(){
                $('#typeSelect').val(data.questionId);
            }, 200)

        });
    },
    getClassify:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            page:1,
            listRows:1000
        };
        Api.adminquestionclasslist(paramsObj, function(data){
            console.log(data);
            _this.getClassifyList(data.arrList)
        });
    },
    getClassifyList:function(data){
        var _this = this;
        if (data && data.length > 0) {
            var html = '<option value="" selected>选择分类</option>', i = 0, len = data.length;
            for (i; i < len; i++) {
                html+='<option value="'+data[i].classId+'">'+data[i].name+'</option>';
            }
            $('#typeSelect').html(html);
            _this.getClassifyEvent();

        }
    },
    getClassifyEvent:function(){
        var _this=this;
        $('#typeSelect').on('change', function(event) {
            event.preventDefault();

            _this.params.classId=$(this).val();
        });
    },
    submit:function(){
        var _this = this;
        $('#submit').click(function(event) {
            var title=$('#helpTitle').val();
            var content=ue.getContent();
            if ($('#is_show').hasClass('switch-off')) {
                var isShow=0;
            }else{
                var isShow=1;
            };
            var paramsObj = {
                passport: _this.params.passport,
                questionId:_this.params.questionId,
                moduleName:'user',
                action:'update',
                classId:_this.params.classId,
                title:title,
                content:content,
                isShow:isShow,
                sort:0
            };
            console.log(paramsObj);
            Api.adminquestionaction(paramsObj, function(data){
                console.log(data);
                layui.use('layer', function(){
                    layer.msg('编辑成功', {icon: 6});
                    setTimeout(function(){
                        window.location.href = Pub.getHtmlUrl('./help-list.html');
                    },1500)
                });
            });
        });
    },
    initEvent: function(){
        var _this = this;
        _this.getClassify();
        _this.getData();
        _this.submit();
    }
};
var page = new Page();